<template>
  <div class="new">
    <!-- <van-notice-bar left-icon="info-o" :scrollable="false" v-if="customerTagArr.length>0">
      <van-swipe vertical class="notice-swipe" :autoplay="3000" :show-indicators="false">
        <van-swipe-item v-for="(item,index) in customerTagArr" :key="index">
          {{ item.sname}}<span style="color:green;margin-left:10ox"> -- {{item.status}}</span>
        </van-swipe-item>
      </van-swipe>
    </van-notice-bar> -->
    <customer-tag-notice :customerTagArr="customerTagArr"></customer-tag-notice>

    <van-overlay :show="overlayShow" :z-index="1000">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>
    <div style="margin-bottom: 10vh;">
      <van-collapse v-model="activeNames">
        <van-collapse-item size="large" v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle"
          :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <idatetime ishead :itemdata="itemdata"
                v-if="itemdata.sUIType == 'datetime' || itemdata.sUIType == 'yearmonth'" :bzjsrqField.sync="bzjsrqField"
                :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" :jsondata="jsondata"
                @dateChange="dateChange" />
              <icode ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'code'" />
              <ienum @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" @changeValue="changeValue"
                v-else-if="itemdata.sUIType == 'enum'" :jsondata="jsondata" :idobject="idobject" />
              <iref @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'ref'"
                :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :clickindexName.sync="clickindexName"
                :clickindex.sync="clickindex" :jsondata="jsondata" :activeNames="activeNames"
                :updateCustomerTagArr="updateCustomerTagArr" />
              <ibool @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata"
                v-else-if="itemdata.sUIType == 'bool'" :jsondata="jsondata" :idobject="idobject" />
              <ifile ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'file'" :jsondata="jsondata" />
              <ipicture ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'picture'" :jsondata="jsondata" />
              <istring @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata"
                v-else-if="itemdata.sUIType == 'string'" :jsondata="jsondata" :idobject="idobject" />
              <imap ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'map'" :jsondata="jsondata"
                :item="item" />
              <istring @handleFiledLink="handleFiledLink" ishead :itemdata="itemdata" v-else :jsondata="jsondata"
                :idobject="idobject" />
            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>

      <!-- 子表 -->
      <body-data :jsondata="jsondata" :detailTitle="detailTitle" :activeBodyNams="activeBodyNams"
        :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :handleFiledLink="handleFiledLink"
        :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :addRow="addRow"
        :delRow="delRow"></body-data>


      <div v-if="notemessage !== ''" style="margin:15px;color:red">{{ notemessage }}</div>
    </div>

    <van-tabbar v-model="active" safe-area-inset-bottom>

      <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed" @click="save">保存</van-tabbar-item>
      <van-tabbar-item v-if="isflow && this.beforeSave && !this.afterSaveSucceed && this.idobject !== 'hdsqd'"
        @click="savesubmit">保存并提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="submit">提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="audit">审核</van-tabbar-item>
      <!--<van-tabbar-item v-if="this.idobject == 'arvouch'" @click="saveAudit">保存并审核</van-tabbar-item>-->
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import * as dd from 'dingtalk-jsapi'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup, DatetimePicker, ActionSheet, Tab, Tabs, Collapse, CollapseItem, CheckboxGroup, Checkbox, Uploader, Overlay, Loading, Rate, NumberKeyboard, Dialog } from 'vant'
import qs from 'qs'
import useInput from '@/utils/useinput'

import { fieldLinkMixins } from '@/mixins/fieldLink.js'
import { fieldMapMixins } from '@/mixins/fieldmap.js'
import { labelBaseMonthMixins } from '@/mixins/labelBaseMonth.js'
import { eventCommonMixins } from '@/mixins/eventCommon.js'
import bodyData from '@/components/bodyData/bodyData'
import CustomerTagNotice from '@/components/CustomerTagNotice'


Vue.use(useInput).use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(CheckboxGroup).use(Checkbox).use(Uploader).use(Overlay).use(Loading).use(Rate).use(NumberKeyboard)
export default {
  components: {
    "body-data": bodyData,
    "customer-tag-notice": CustomerTagNotice
  },
  name: 'new',
  mixins: [fieldLinkMixins, fieldMapMixins, eventCommonMixins, labelBaseMonthMixins],
  computed: {
    tableHead() {
      const res = bodydata.value.map((item, index) => {
        const obj = {};
        for (let i = 0; i < item.length; i++) {
          obj[item[i].fieldID] = item[i].value;
        }
        return obj;
      });
      return res;
    }


  },

  data() {
    let _this = this
    return {

      bodyDataShowType: "row",
      idobject: this.$route.query.idobject,
      idtemplate: this.$route.query.idtemplate,
      ajlsname: this.$route.query.ajlsname,
      newsname: this.$route.query.newsname,
      jsondata: {
        bodydata: {},
        body: [],
        bodymetadata: {},
        headmetadata: {}
      },
      index: 0,
      active: 0,
      activeNames: [],
      activeBodyNams: [],
      isLoad: false,
      conditionValHidden: '',
      clickmodel: {},
      clickfrom: '',
      clickindexName: '',
      clickindex: 0,
      isDisable: false,
      overlayShow: false,
      notemessage: '',
      isflow: false,
      bzjsrqField: '',
      xzksrqField: '',
      xzjsrqField: '',
      solrLoading: true,
      solrFinished: false,
      solrList: [],
      solrListDivStyle: {
        display: 'none'
      },
      events: {
        init: (o) => {
          // //console.log(o.getCenter())
          _this.mapInstance = o
          let that = this
          // //console.log('this:  ' + that)
          if (_this.isPosi === 0) {
            AMap.plugin('AMap.Geolocation', function () {
              var geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,
                timeout: 10000,
                zoomEnable: false,
                dragEnable: false,
                showButton: false,
                buttonOffset: new AMap.Pixel(10, 20),
                zoomToAccuracy: true
              })
              o.addControl(geolocation)
              geolocation.getCurrentPosition(function (status, result) {
                if (status === 'complete') {
                  o.center = result.position
                  // //console.log(result.position + '-------')
                  // //console.log(result.formattedAddress)
                  that.place = result.formattedAddress
                  for (let groupindex in _this.jsondata.head) {
                    let itemgroup = _this.jsondata.head[groupindex].groupvalue
                    for (let itemindex in itemgroup) {
                      let itemdata = itemgroup[itemindex]
                      if (itemdata.sUIType === 'map') {
                        itemdata.value = result.formattedAddress + '&' + result.position
                      }
                    }
                  }
                } else {
                  alert(JSON.stringify(result))
                  // //console.log(result)
                }
              })
            })
          }
        }
      },
      // 以下两个状态判断是否保存, 来决定显示保存按钮 后者是 保存并审核 保存并提交 按钮
      // 未保存
      beforeSave: true,
      // 保存成功
      afterSaveSucceed: false,
      isDirectConnection: false

    }
  },
  watch: {
    isDirectConnection(value) {
      if (!value) {
        this.handleFiledNoMust()
      }
    }

  },
  computed: {
    mydata() {
      // //console.log('jsondata', this.jsondata)
      // //console.log('head', this.jsondata.head)
      //console.log('starting mydata')
      let flag = false
      this.isDirectConnection = false
      for (let i = 0; this.jsondata.head && i < this.jsondata.head.length; i++) {
        let gv = this.jsondata.head[i].groupvalue
        for (let j = 0; gv && j < gv.length; j++) {
          if (gv[j].sField == 'isdls1' && gv[j].value == '是') {
            flag = true
            this.isDirectConnection = true
            break
          }
        }

      }
      //console.log('flag', flag)
      //console.log('isDirectConnection', this.isDirectConnection)

      let newhead = []
      newhead = this.jsondata.head.filter(item => {
        if (flag) {
          // if (item.grouptitle === '直连信息') {
          // }
          return true
        }

        if (!flag) {
          // if (item.grouptitle === '直连信息') {
          //   item.groupvalue.forEach(i => {
          //     i.lMustInput = '0'
          //   })
          // }
          return item.grouptitle != '直连信息'
        }
      })
      return newhead
    }
  },
  created() {
    // //console.log('created......')
    // this.fetchData()
    let _this = this
    document.title = this.$route.query.stitle
    // //console.log('document.title', document.title)
    if (dd.env.platform !== 'notInDingTalk') {
      dd.ready(function () {
        dd.biz.navigation.setTitle({
          title: _this.$route.query.stitle
        })
      })
    }
  },


  mounted: function () {
    if (!this.isLoad) {
      let data = {
        idtemplate: this.idtemplate,
        ajlsname: this.ajlsname,
        newsname: this.newsname
      }
      let url = '/api/' + this.idobject + '.new.do'
      if (this.$route.query.skdtid && this.$route.query.operation) {
        url = '/api/skd.rk.do'
        data = {
          skdtid: this.$route.query.skdtid,
          idtemplate: this.idtemplate
        }
      }
      this.axios({
        method: 'post',
        url: url,
        data: qs.stringify(data)
      }).then(async res => {
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          this.notemessage = res.data.notemessage
          this.isflow = res.data.isflow
          this.jsondata = res.data.jsondata

          // 字段联动变量
          this.orginData = JSON.parse(JSON.stringify(this.jsondata))
          this.controlLinkArr = this.getFieldLink(this.jsondata)
          if (this.jsondata.hasdetail != '0') {
            this.controlLinkArrDetail = this.getFieldLinkDetail(this.jsondata)
          }
          // 字段联动初始化
          this.firstHandleFiledLink(this.jsondata)

          // //console.log('action jsondata', this.jsondata)
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            let head = res.data.jsondata.head[i]
            this.activeNames.push(head.grouptitle)
            if (this.idobject === 'weekreports') {
              for (let j = 0; j < head.groupvalue.length; j++) {
                let rec = head.groupvalue[j]
                if (rec.sField === 'bzjsrq') {
                  this.bzjsrqField = rec.value
                }
                if (rec.sField === 'xzksrq') {
                  this.xzksrqField = rec.value
                }
                if (rec.sField === 'xzjsrq') {
                  this.xzjsrqField = rec.value
                }

              }
            }
          }

          if (this.jsondata.body) {
            let k = 0
            for (const [key, value] of Object.entries(this.jsondata.body)) {
              this.activeBodyNams.push(`${key}${k}`)
              k++
            }
          }



          // 健民特判
          if (this.idobject == 'account1' || this.idobject == 'account2' || this.idobject == 'account4' || this.idobject == 'accountrxkh' || this.idobject == 'accountxykh' || this.idobject == 'accounthbkh' || this.idobject == 'accountotc' || this.idobject == 'accountzdkh' || this.idobject == 'accountlszb') {
            let zrfjflag = true
            for (let i = 0; i < this.jsondata.head.length; i++) {
              if (this.jsondata.head[i].grouptitle == '行业库信息') {
                for (let j = 0; j < this.jsondata.head[i].groupvalue.length; j++) {
                  if (this.jsondata.head[i].groupvalue[j].sTitle == '行业库客户名称' && this.jsondata.head[i].groupvalue[j].value) {
                    zrfjflag = false
                    // this.jsondata.head[0].groupvalue[11].lCanInput = '1'
                    // this.jsondata.head[0].groupvalue[11].lMustInput = '1'
                  }
                }
              }
            }
            for (let i = 0; i < this.jsondata.head.length; i++) {
              if (this.jsondata.head[i].grouptitle == '基本信息') {
                for (let j = 0; j < this.jsondata.head[i].groupvalue.length; j++) {
                  if (this.jsondata.head[i].groupvalue[j].sTitle == '准入附件') {
                    if (zrfjflag) {
                      this.jsondata.head[i].groupvalue[j].lCanInput = '1'
                      this.jsondata.head[i].groupvalue[j].lMustInput = '1'
                    } else {
                      this.jsondata.head[i].groupvalue[j].lCanInput = '0'
                      this.jsondata.head[i].groupvalue[j].lMustInput = '0'
                    }

                  }
                }
              }
            }
          }

          // for (let groupindex in this.jsondata.head) {
          //   for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
          //     let item = this.jsondata.head[groupindex].groupvalue[itemindex];
          //     if (item.sField === "idowner") {
          //       item.value = res.data.usercode
          //       item.valuehidden = res.data.userid
          //       const data = {
          //         ref_object: item.idJoinObject,
          //         ref_id: item.valuehidden,
          //         ref_toobject: this.$route.params.tobo,
          //         ref_tofieldas: item.sField,
          //         ref_tofield: item.sField,
          //         ref_classname: item.classname,
          //       }
          //       this.axios({
          //         method: 'post',
          //         url: '/api/ref.select.do',
          //         data: qs.stringify(data)
          //       }).then(res2 => {
          //         const storeRefObj = {
          //           ref_id: item.id,
          //           ref_show: item.value || '',
          //           ref_field: item.sField,
          //           ref_item: item
          //         }
          //         const outdata = res2.data.jsondata.outdata
          //         if (Object.getOwnPropertyNames(outdata).length > 2) {
          //           storeRefObj.ref_map = outdata
          //           storeRefObj.ref_toobject = this.$route.params.tobo

          //         }
          //         this.$store.commit('refReBack', storeRefObj)
          //       })

          //     }
          //   }
          // }
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  },
  methods: {
    getRefSelect(item) {
      const data = {
        ref_object: item.idJoinObject,
        ref_id: item.valuehidden,
        ref_toobject: this.$route.params.tobo,
        ref_tofieldas: item.sField,
        ref_tofield: item.sField,
        ref_classname: item.classname,
      }
      this.axios({
        method: 'post',
        url: '/api/ref.select.do',
        data: qs.stringify(data)
      })
    },
    // 经销商授权授权类型联动
    changeValue(clickmodel) {
      if (clickmodel.idObject === 'jxssqb' && clickmodel.sField === 'sqlx') {
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField == 'edate') {
              if (clickmodel.value == '年度一级经销商授权' || clickmodel.value == '年度物流配送商授权') {
                if (String(new Date().getMonth() + 1).padStart(2, '0') >= 10) {
                  item.value = ''
                } else {
                  item.value = `${new Date().getFullYear()}-12-31`
                }
              } else if (clickmodel.value == '临时一级经销商授权' || clickmodel.value == '临时物流配送商授权') {
                item.value = new Date(Date.now() + 90 * 86400000).toISOString().split('T')[0]
              }
            }
            if (item.sField == 'sqlx') {
              if (String(new Date().getMonth() + 1).padStart(2, '0') >= 10 && (clickmodel.value == '年度一级经销商授权' || clickmodel.value == '年度物流配送商授权')) {
                item.value = ''
                item.valuehidden = ''
                return Toast('10月1日及之后，不允许申请年度授权，只能申请临时授权')
              }
            }
          }
        }
      }
    },
    handleFiledNoMust() {
      this.jsondata.head = this.jsondata.head.map(item => {
        if (item.grouptitle === '直连信息') {
          for (let i = 0; i < item.groupvalue.length; i++) {
            item.groupvalue[i].value = ''
            item.groupvalue[i].lMustInput = '0'
          }
          return item
        } else {
          return item
        }
      })
    },

    handleChange(formItem, value, valuehidden = "") {
      formItem.value = value;
      if (valuehidden) {
        formItem.valuehidden = valuehidden
      }


    },
    handleCellClick({ row, column, rowIndex, columnIndex }) {
      //console.log({ row, column, rowIndex, columnIndex })
    },
    detailTitle(indexName, item, index) {
      for (let i = 0; i < item.length; i++) {
        if (item[i].sField == this.jsondata.titleField) {
          return (index + 1) + '.' + (item[i].value.length > 0 ? item[i].value : '新' + indexName)
        }
      }
      return indexName + (index + 1)
    },
    save() {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        let msgall = ''
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    msgall += itemdata.sField + ' '
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    msgall += itemdata.sField + ' '
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    alert(itemdata.sTitle + '不能为空')
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    msgall += itemdata.sField + ' '
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }

        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请检查必输项!')
          //console.log(msgall, this.jsondata)
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast('保存成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })

              this.$router.go(-1)
              return
            }
            // this.$router.push({ name: 'list', query: { idobject: this.idobject } })
            // this.$router.go(-1)
            this.$router.push({
              path: 'card',
              query: {
                itemid: this.jsondata[this.idobject + '_id'],
                idobject: this.idobject,
                idtemplate: this.$route.query.idtemplate
              }
            })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          // //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          // //console.log(res)
        })
      }
    },
    submit() {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        let msgall = ''
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    msgall += itemdata.sField + ' '
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    msgall += itemdata.sField + ' '
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    alert(itemdata.sTitle + '不能为空')
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    msgall += itemdata.sField + ' '
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        // //console.log('jsondata', this.jsondata)
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请检查必输项!')
          //console.log(msgall, this.jsondata)
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.flowsubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast(res.data.des + " ");
            if (res.data.des.indexOf("成功") < 0) return
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('提交成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          // //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          // //console.log(res)
        })
      }
    },
    savesubmit() {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    alert(itemdata.sTitle + '不能为空')
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.isDisable = true
            this.overlayShow = true
            // this.beforeSave = false
            // this.afterSaveSucceed = true
            Toast('保存成功...开始提交...')
            // 发起提交请求开始 --
            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.flowsubmit.do',
              data: qs.stringify(data)
            }).then(res => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                Toast(res.data.des + " ");
                if (res.data.des.indexOf("成功") < 0) {
                  this.$router.push({
                    path: 'card',
                    query: {
                      itemid: this.jsondata[this.idobject + '_id'],
                      idobject: this.idobject,
                      idtemplate: this.$route.query.idtemplate
                    }
                  })
                  return
                }
                Toast('提交成功')
                if (this.$route.query.newflag === 'refnew') {
                  this.$store.commit('refReBack', { ref_show: 'true' })
                  this.$router.go(-1)
                  return
                }
                this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
              // //console.log(res)
            }).catch(res => {
              this.isDisable = false
              this.overlayShow = false
              // //console.log(res)
            })

            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          // //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          // //console.log(res)
        })
      }
    },
    saveAudit() {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let i = 0; i < this.jsondata.bodydata.length; i++) {
            let item = this.jsondata.bodydata[i]
            for (let itemindex in item) {
              let itemdata = item[itemindex]
              if (itemdata.sUIType === 'ref') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
              } else if (itemdata.sUIType === 'enum') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
              } else {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsaveaudit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast('保存并审核成功')
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          // //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          // //console.log(res)
        })
      }
    },
    addRow(indexName) {
      let bodycopy = JSON.parse(JSON.stringify(this.jsondata.body[indexName]))
      bodycopy.forEach((iv) => {
        iv.copyTitle = iv.sTitle
        iv.sTitle = this.replaceNWithMonth(iv.sTitle, this.monthMix);
      });
      this.jsondata.bodydata[indexName].push(bodycopy)
      this.orginData.bodydata[indexName].push(bodycopy)
      //console.log("*****", this.jsondata)
    },
    delRow(indexName, index) {
      if (this.jsondata.bodydata[indexName].length === 1) {
        Toast('至少保留一条表体行，不能删除！')
        return
      }

      Dialog.confirm({
        title: '',
        message: '你确定要删除' + indexName + ' ' + (index + 1) + '吗?'
      })
        .then(() => {
          // on confirm
          this.jsondata.bodydata[indexName].splice(index, 1)
        })
        .catch(() => {
          // on cancel
        })
    },
    toggle(index) {
      this.$refs.checkboxes[index].toggle()
    }
  }
  // activated() {
  //   if (this.$store.state.ref_show !== '') {
  //     this.clickmodel.value = this.$store.state.ref_show
  //     // //console.log("%%%%",this.clickmodel)

  //     this.clickmodel.value = this.$store.state.ref_show
  //     this.clickmodel.valuehidden = this.$store.state.ref_id
  //     this.clickmodel.errormsg = ''

  //     if (this.$store.state.ref_map) {
  //       if (this.clickfrom === 'headdata') {
  //         this.handleRefMap('headdata', this.jsondata.head, this.$store.state.ref_map)
  //       } else if (this.clickfrom === 'bodydata') {
  //         this.handleRefMap('bodydata', this.jsondata.bodydata[this.clickindexName][this.clickindex], this.$store.state.ref_map)
  //       }
  //     }
  //     // return

  //     this.clickmodel = {}
  //     this.clickindex = 0
  //     this.clickindexName = ''
  //     this.clickfrom = ''
  //     this.$store.commit('clearRef')

  //     /* let fieldmap = this.clickmodel.fieldmap
  //     // //console.log('fieldmap', fieldmap)

  //     if (fieldmap !== undefined && fieldmap.length > 0) {
  //       let arrFieldmap = fieldmap.split(';')
  //       let item = this.$store.state.ref_item
  //       if (this.clickfrom === 'headdata') {
  //         a.aa = 'bbb'
  //         for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
  //           let itemgroup = this.jsondata.head[groupindex]
  //           for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
  //             let itemdata = itemgroup.groupvalue[itemindex]
  //             for (let fieldmapindex in arrFieldmap) {
  //               let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
  //               if (this.$store.state.ref_field === arrFieldMapKey[1]) {
  //                 continue
  //               }
  //               if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
  //                 continue
  //               }
  //               if (itemdata.sField === arrFieldMapKey[1]) {
  //                 if (itemdata.sUIType === 'ref') {
  //                   itemdata.value = item[arrFieldMapKey[0] + '_ref']
  //                   itemdata.valuehidden = item[arrFieldMapKey[0]]
  //                 } else if (itemdata.sUIType === 'enum') {
  //                   itemdata.value = item[arrFieldMapKey[0] + '_enum']
  //                   itemdata.valuehidden = item[arrFieldMapKey[0]]
  //                 } else {
  //                   itemdata.value = item[arrFieldMapKey[0]]
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       } else if (this.clickfrom === 'bodydata') {
  //         a.aa = 'ccc'
  //         for (let itemindex in this.jsondata.bodydata[this.clickindexName][this.clickindex]) {
  //           let itemdata = this.jsondata.bodydata[this.clickindexName][this.clickindex][itemindex]
  //           for (let fieldmapindex in arrFieldmap) {
  //             let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
  //             if (this.$store.state.ref_field === arrFieldMapKey[1]) {
  //               continue
  //             }
  //             if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
  //               continue
  //             }
  //             if (itemdata.sField === arrFieldMapKey[1]) {
  //               if (itemdata.sUIType === 'ref') {
  //                 itemdata.value = item[arrFieldMapKey[0] + '_ref']
  //                 itemdata.valuehidden = item[arrFieldMapKey[0]]
  //               } else if (itemdata.sUIType === 'enum') {
  //                 itemdata.value = item[arrFieldMapKey[0] + '_enum']
  //                 itemdata.valuehidden = item[arrFieldMapKey[0]]
  //               } else {
  //                 itemdata.value = item[arrFieldMapKey[0]]
  //               }
  //             }
  //           }
  //         }
  //       }
  //     }
  //     // //console.log('a', a)
  //     // //console.log('clickmodel', this.clickmodel)
  //     // //console.log('clickindex', this.clickindex)
  //     // //console.log('clickindexName', this.clickindexName)
  //     // //console.log('clickfrom', this.clickfrom)
  //     this.clickmodel = {}
  //     this.clickindex = 0
  //     this.clickindexName = ''
  //     this.clickfrom = ''
  //     this.$store.commit('clearRef') */
  //   }
  // }
}
</script>

<style>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}

.van-collapse-item__content {
  padding: 0px 0px;
}
</style>
